﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Graphics.PackedVector;



namespace WindowsGame
{
    public class HeadUpDisplayRenderer
    {
        SpriteBatch spriteBatch;

        HeadUpDisplayResources headUpDisplayResources;

        StringBuilder staticText = new StringBuilder("FPS:\nFPS Average:\nMillisecs Per Frame:");
        StringBuilder dinamicBuffer = new StringBuilder("---\n---\n---");

        Color staticTextColor = Color.White;
        Color dinamicBufferColor = Color.White;

		IHeadUpDisplayLogic headUpDisplayLogic;



        public HeadUpDisplayRenderer(SpriteBatch spriteBatchIn, HeadUpDisplayResources headUpDisplayResourcesIn,
			IHeadUpDisplayLogic headUpDisplayLogicIn)
        {
            spriteBatch = spriteBatchIn;
            headUpDisplayResources = headUpDisplayResourcesIn;
			headUpDisplayLogic = headUpDisplayLogicIn;
        }



        public void Draw(GameTime gameTime)
        {
            Debug.Assert(spriteBatch != null);
            Debug.Assert(headUpDisplayResources != null);
			Debug.Assert(headUpDisplayLogic != null);

			if(headUpDisplayLogic.Display)
            {
				double averageFPS = headUpDisplayLogic.AverageFPS;

				dinamicBuffer.Clear();
				dinamicBuffer.AppendLine(headUpDisplayLogic.FramePerSeconds.ToString("F2"));
				dinamicBuffer.AppendLine(averageFPS.ToString("F2"));
				dinamicBuffer.AppendLine(headUpDisplayLogic.MillisecsPerFrame.ToString("F2"));

				if(averageFPS < 24)
					dinamicBufferColor = Color.OrangeRed;
				else if(averageFPS < 30)
					dinamicBufferColor = Color.Yellow;
				else
					dinamicBufferColor = Color.GreenYellow;

                spriteBatch.Begin();
                spriteBatch.DrawString(headUpDisplayResources.spriteFont, staticText, new Vector2(4.0f, 0.0f),
                    staticTextColor);
                spriteBatch.DrawString(headUpDisplayResources.spriteFont, dinamicBuffer, new Vector2(144.0f, 0.0f),
                    dinamicBufferColor);
                spriteBatch.End();
            }
        }
    }
}